﻿package Classes {
	
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.geom.Point;
	
	public class CoverForMouse extends MovieClip {
		
		
		public function CoverForMouse() {
			this.addEventListener(Event.ADDED_TO_STAGE,addToStage);
		}
		
		private function addToStage(e:Event)
		{
			setEvents();
		}
		
		//transform row,col to postion
		public function rowcolToPostion(r:int,c:int):Point
		{
			var instName:String = "p_"+r+"_"+c;
			var chessBlank = getChildByName(instName);
			if(chessBlank != null) {
				return new Point(chessBlank.x,chessBlank.y);
			}
			return new Point(0,0);
		}
		
		private function setEvents() {
			for (var i  = 0;i<this.numChildren;i++) {
				var chessBlank:Object = getChildAt(i);
				if(chessBlank is MovieClip) {
					chessBlank.addEventListener(MouseEvent.CLICK,chessClick);
				}
			}
		}
		
		private function chessClick(e:MouseEvent) {
			var chessBlank:MovieClip = e.currentTarget as MovieClip;
			var posInfo:Array = chessBlank.name.split("_");
			if(posInfo.length>2) {
				var row:int = parseInt(posInfo[1]);
				var col:int = parseInt(posInfo[2]);
				Main.instance.board.clickBoard(row,col);
			}
		}
	}
	
}
